package switchviz.graphic.gauge.graph2d.scale;

import java.util.StringTokenizer;

import org.jdom.Element;

public class Graph2dBoardPersistence 
{
	private final static String NUMSMALLTICKS = "numsmallticks";
	private final static String NUMBIGTICKS = "numbigticks";
	private final static String LENGTHBIGTICKS = "lengthbigticks";
	private final static String LENGTHSMALLTICKS = "lengthsmallticks";
	private final static String WIDTHBIGTICKS = "widthbigticks";
	private final static String WIDTHSMALLTICKS = "widthsmallticks";
	private final static String SCALECOLOR = "scalecolor";
	private final static String LABELCOLOR = "labelcolor";

	private Graph2dBoardDataModel _scalemodel = null;
	private Element _root = null;
	
	public Graph2dBoardDataModel getDataModelFromElement()
	{
		if(_root.getChild(NUMBIGTICKS) != null)
			getDataModel().setNumbigticks(Integer.valueOf(_root.getChildText(NUMBIGTICKS)));
		if(_root.getChild(NUMSMALLTICKS) != null)
			getDataModel().setNumsmallticks(Integer.valueOf(_root.getChildText(NUMSMALLTICKS)));
		if(_root.getChild(LENGTHBIGTICKS) != null)
			getDataModel().setLengthbigticks(Float.valueOf(_root.getChildText(LENGTHBIGTICKS)));
		if(_root.getChild(LENGTHSMALLTICKS) != null)
			getDataModel().setLengthsmallticks(Float.valueOf(_root.getChildText(LENGTHSMALLTICKS)));
		if(_root.getChild(WIDTHBIGTICKS) != null)
			getDataModel().setWidthbigticks(Float.valueOf(_root.getChildText(WIDTHBIGTICKS)));
		if(_root.getChild(WIDTHSMALLTICKS) != null)
			getDataModel().setWidthsmallticks(Float.valueOf(_root.getChildText(WIDTHSMALLTICKS)));
		if(_root.getChild(SCALECOLOR) != null)
			getDataModel().setScalecolor(getColorCode(_root.getChildText(SCALECOLOR)));
		if(_root.getChild(LABELCOLOR) != null)
			getDataModel().setLabelcolor(getColorCode(_root.getChildText(LABELCOLOR)));
		return getDataModel();
	}
	
	public Element setDataModel(Graph2dBoardDataModel scalemodel)
	{
		_scalemodel = scalemodel;
		return saveDataModel();
	}
	
	
	public Graph2dBoardPersistence(Element root)
	{
		_root = root;
	}
	
	private Graph2dBoardDataModel getDataModel()
	{
		if (_scalemodel == null)
			_scalemodel = new Graph2dBoardDataModel();
		return _scalemodel;
	}
	
	private void addNode(String nodename, String value)
	{
		if(_root.getChild(nodename) != null)
			_root.getChild(nodename).setText(value);
		else
			_root.addContent(new Element(nodename).setText(value));
	}
	
	private float[] getColorCode(String colorcode)
	{
		float[] color = {0.0f, 0.0f, 0.0f};
		StringTokenizer tokenizer = new StringTokenizer(colorcode, ";", false);
		int i = 0;
		while(tokenizer.hasMoreTokens())
		{
			if(i == 3) break;
			color[i] = Float.valueOf(tokenizer.nextToken());
			++i;
		}
		return color;
	}
	
	private Element saveDataModel()
	{
		addNode(NUMBIGTICKS, String.valueOf(getDataModel().getNumbigticks()));
		addNode(NUMSMALLTICKS, String.valueOf(getDataModel().getNumsmallticks()));
		addNode(LENGTHBIGTICKS, String.valueOf(getDataModel().getLengthbigticks()));
		addNode(LENGTHSMALLTICKS, String.valueOf(getDataModel().getLengthsmallticks()));
		addNode(WIDTHBIGTICKS, String.valueOf(getDataModel().getWidthbigticks()));
		addNode(WIDTHSMALLTICKS, String.valueOf(getDataModel().getWidthsmallticks()));
		String colorcode = getDataModel().getScalecolor()[0] + ";" + getDataModel().getScalecolor()[1] + ";" + getDataModel().getScalecolor()[2];
		addNode(SCALECOLOR, colorcode);
		colorcode = getDataModel().getLabelcolor()[0] + ";" + getDataModel().getLabelcolor()[1] + ";" + getDataModel().getLabelcolor()[2];
		addNode(LABELCOLOR, colorcode);
		
		return _root;
	}
}
